package com.mea.client;

import org.kalmeo.kuix.core.Kuix;
import org.kalmeo.kuix.widget.Screen;
import org.kalmeo.util.frame.Frame;

import java.io.IOException;

import javax.microedition.io.StreamConnection;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;

import javax.microedition.io.Connector;


public class SearchServerFrame implements Frame {

    String serverURL = null; // = "btgoep://0019639C4007:6";
    StreamConnection stream = null;
    OutputStream out = null;
    boolean play = false;
	
	public void onAdded() {
		// Load the content from the XML file with Kuix.loadScreen static method
	    Screen screen = Kuix.loadScreen("searchserver.xml", null);

	    // Set the application current screen  
	    screen.setCurrent();
	/*
	    // Search for the server
	    if (serverURL == null) {
            String[] searchArgs = null;
            // Connect to OBEXPutServer from examples
            // searchArgs = new String[] { "11111111111111111111111111111123" };
            try {
				ServicesSearch.main(searchArgs);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
            if (ServicesSearch.serviceFound.size() == 0) {
                System.out.println("OBEX service not found");

                return;
            }
            // Select the first service found
            serverURL = (String)ServicesSearch.serviceFound.elementAt(0);
        }

	    // Connect to server
        System.out.println("Connecting to " + serverURL);

        try {
			stream = (StreamConnection) Connector.open(serverURL);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        try {
			out = stream.openOutputStream();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		*/
		Kuix.getFrameHandler().pushFrame(new ServerFoundFrame(stream, out));
	}

	public boolean onMessage(Object arg0, Object[] arg1) {
		// TODO Auto-generated method stub
		return false;
	}

	public void onRemoved() {
		// TODO Auto-generated method stub
		
	}

}
